package android.slc.appdb.sys.service;


import android.slc.appdb.sys.entity.CsDictData;
import android.slc.box.db.service.DbEntityMapper;
import cn.hutool.core.collection.CollectionUtil;
import io.reactivex.rxjava3.annotations.NonNull;

import java.util.Comparator;
import java.util.List;

/**
 * 字段数据Mapper
 */
public interface CsDictDataMapper extends DbEntityMapper<CsDictData> {

    @Override
    default List<CsDictData> getAllList() {
        List<CsDictData> csDictDataList = DbEntityMapper.super.getAllList();
        CollectionUtil.sort(csDictDataList, Comparator.comparingInt(CsDictData::getSort));
        return csDictDataList;
    }

    /**
     * 根据字典类型和值查找字典数据
     *
     * @param dictType
     * @param dictValue
     * @return
     */
    CsDictData findDictDataByDictType(@NonNull String dictType, @NonNull String dictValue);

    /**
     * 根据字典类型查找字典数据列表
     *
     * @param dictType
     * @return
     */
    List<CsDictData> findDictDataListByDictType(@NonNull String dictType);

    /**
     * 根据字典类型更新字典
     *
     * @param oldDictType
     * @param newDictType
     */
    default void updateDateType(@NonNull String oldDictType, @NonNull String newDictType) {
        List<CsDictData> dictDataList = findDictDataListByDictType(oldDictType);
        if (dictDataList != null) {
            for (CsDictData csDictData : dictDataList) {
                csDictData.setDictType(newDictType);
            }
            put(dictDataList);
        }
    }

    /**
     * 根据类型移除
     *
     * @param dictType
     */
    default void removeByType(@NonNull String dictType) {
        List<CsDictData> dictDataList = findDictDataListByDictType(dictType);
        remove(dictDataList);
    }
}
